#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
AI童话书配置文件
"""

import os
from dataclasses import dataclass
from typing import Dict, List

@dataclass
class ModelConfig:
    """模型配置"""
    model_path: str = "./ERNIE-4.5-0.3B-Paddle"
    model_name: str = "ERNIE-4.5-0.3B-Paddle"
    max_length: int = 512
    temperature: float = 0.8
    top_p: float = 0.9

@dataclass
class KnowledgeConfig:
    """知识库配置"""
    zhiyuan_api_url: str = "https://api.zhiyuan.org"
    knowledge_domains: List[str] = None
    cache_enabled: bool = True
    cache_duration: int = 3600

    def __post_init__(self):
        if self.knowledge_domains is None:
            self.knowledge_domains = [
                "science", "history", "literature", "mathematics", 
                "nature", "social", "psychology", "education"
            ]

@dataclass
class WebConfig:
    """Web应用配置"""
    host: str = "0.0.0.0"
    port: int = 5000
    debug: bool = True
    secret_key: str = "fairy_tale_secret_key_2024"
    session_timeout: int = 3600

@dataclass
class StoryConfig:
    """故事配置"""
    max_story_length: int = 10
    min_choices_per_node: int = 2
    max_choices_per_node: int = 4
    difficulty_levels: List[str] = None
    age_groups: List[str] = None

    def __post_init__(self):
        if self.difficulty_levels is None:
            self.difficulty_levels = ["简单", "中等", "困难", "专家", "大师"]
        
        if self.age_groups is None:
            self.age_groups = ["3-6", "7-10", "11-14"]

class AppConfig:
    """应用总配置"""
    
    def __init__(self):
        self.model = ModelConfig()
        self.knowledge = KnowledgeConfig()
        self.web = WebConfig()
        self.story = StoryConfig()
        
        # 从环境变量加载配置
        self._load_from_env()
    
    def _load_from_env(self):
        """从环境变量加载配置"""
        self.model.model_path = os.getenv("ERNIE_MODEL_PATH", self.model.model_path)
        self.model.temperature = float(os.getenv("MODEL_TEMPERATURE", self.model.temperature))
        self.web.host = os.getenv("WEB_HOST", self.web.host)
        self.web.port = int(os.getenv("WEB_PORT", self.web.port))
        self.web.debug = os.getenv("WEB_DEBUG", "true").lower() == "true"
        self.knowledge.zhiyuan_api_url = os.getenv("ZHIYUAN_API_URL", self.knowledge.zhiyuan_api_url)